<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Japanese_Bakery extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	public function index()
	{
		if($this->session->userdata('admin_logged_in') == FALSE){
			redirect(base_url()."admin/login");
			exit();
		}
		
		$this->load->model('admin/jp_bakery_model','jp_bakery_model');
		$result['news_list'] = $this->jp_bakery_model->lists(1,50);
		
		$data['menu_1'] = '';
		$data['menu_2'] = '';
		$data['menu_3'] = '';
		$data['menu_4'] = 'current';
		$data['menu_5'] = '';
		$this->load->view('admin/b_header_view');
		$this->load->view('admin/b_menu_view',$data);
		$this->load->view('admin/b_japanese_bakery_view' , $result);
		$this->load->view('admin/b_footer_view');
	}
	
	public function lists($p=1)
	{
		if($this->session->userdata('admin_logged_in') == FALSE){
			redirect(base_url()."admin/login");
			exit();
		}
		
		$this->load->model('admin/jp_bakery_model','jp_bakery_model');
		$result['news_list'] = $this->jp_bakery_model->lists($p,50);
		
		$data['menu_1'] = '';
		$data['menu_2'] = '';
		$data['menu_3'] = '';
		$data['menu_4'] = 'current';
		$data['menu_5'] = '';
		$this->load->view('admin/b_header_view');
		$this->load->view('admin/b_menu_view',$data);
		$this->load->view('admin/b_japanese_bakery_view' , $result);
		$this->load->view('admin/b_footer_view');
	}
	
	public function add()
	{
		if($this->session->userdata('admin_logged_in') == FALSE){
			redirect(base_url()."admin/login");
			exit();
		}
		
		$data['menu_1'] = '';
		$data['menu_2'] = '';
		$data['menu_3'] = '';
		$data['menu_4'] = 'current';
		$data['menu_5'] = '';
		$this->load->view('admin/b_header_view');
		$this->load->view('admin/b_menu_view',$data);
		$this->load->view('admin/b_japanese_bakery_add_view');
		$this->load->view('admin/b_footer_view');
	}
	
	public function add_process()
	{
		if($this->session->userdata('admin_logged_in') == FALSE){
			redirect(base_url()."admin/login");
			exit();
		}
		
		if(isset($_POST['jpb_en']) AND $_POST['jpb_en']!='' AND isset($_POST['jpb_jp']) AND $_POST['jpb_jp']!=''){
			$input['jpb_en'] = $_POST['jpb_en'];
			$input['jpb_jp'] = $_POST['jpb_jp'];
			$this->load->model('admin/jp_bakery_model','jp_bakery_model');
			$id = $this->jp_bakery_model->add($input);
			
			if($id!=0){
				if(isset($_FILES["img"]) && is_uploaded_file($_FILES["img"]["tmp_name"]) && $_FILES["img"]["tmp_name"]!=''){
					$this->load->library('ciupload');
					$this->ciupload->Upload($_FILES['img']);
					if ($this->ciupload->uploaded){
						$this->ciupload->image_resize = true;
						$this->ciupload->image_ratio_fill = true;
						$this->ciupload->image_x = 600;
						$this->ciupload->image_y = 852;
						$this->ciupload->Process(APPPATH.'../assets/image/japanese-bakery/'.$id);
						
						if ($this->ciupload->processed)
						{
							$input['id'] = $id;
							$input['img'] = $this->ciupload->file_dst_name;
							$result = $this->jp_bakery_model->update_img($input);
							if($result==0)
							{
								@unlink(APPPATH.'../assets/image/japanese-bakery/'.$id.'/'.$this->ciupload->file_dst_name);
							}
						}
						$this->ciupload->clean();
					}
					redirect(base_url()."admin/japanese-bakery");
				}else{
					redirect(base_url()."admin/japanese-bakery");
				}
			}else{
				redirect(base_url()."admin/japanese-bakery");
			}
		}else{
			
			redirect(base_url()."admin/japanese-bakery/add");
		}
	}
	
	public function edit($id)
	{
		if($this->session->userdata('admin_logged_in') == FALSE){
			redirect(base_url()."admin/login");
			exit();
		}
		
		$this->load->model('admin/jp_bakery_model','jp_bakery_model');
		$result['info'] = $this->jp_bakery_model->get_info($id);
		
		$data['menu_1'] = '';
		$data['menu_2'] = '';
		$data['menu_3'] = '';
		$data['menu_4'] = 'current';
		$data['menu_5'] = '';
		$this->load->view('admin/b_header_view');
		$this->load->view('admin/b_menu_view',$data);
		$this->load->view('admin/b_japanese_bakery_edit_view',$result);
		$this->load->view('admin/b_footer_view');
	}
	
	public function edit_process()
	{
		if($this->session->userdata('admin_logged_in') == FALSE){
			redirect(base_url()."admin/login");
			exit();
		}
		
		if(isset($_POST['id']) AND isset($_POST['jpb_en']) AND $_POST['jpb_en']!='' AND isset($_POST['jpb_jp']) AND $_POST['jpb_jp']!=''){
			$id = $_POST['id'];
			$input['id'] = $id;
			$input['jpb_en'] = $_POST['jpb_en'];
			$input['jpb_jp'] = $_POST['jpb_jp'];
			$this->load->model('admin/jp_bakery_model','jp_bakery_model');
			$check = $this->jp_bakery_model->update($input);
			
			if($check){
				if(isset($_FILES["img"]) && is_uploaded_file($_FILES["img"]["tmp_name"]) && $_FILES["img"]["tmp_name"]!=''){
					$this->load->library('ciupload');
					$this->ciupload->Upload($_FILES['img']);
					if ($this->ciupload->uploaded){
						$this->ciupload->image_resize = true;
						$this->ciupload->image_ratio_fill = true;
						$this->ciupload->image_x = 600;
						$this->ciupload->image_y = 852;
						$this->ciupload->Process(APPPATH.'../assets/image/japanese-bakery/'.$id);
						
						if ($this->ciupload->processed){
							$input['id'] = $id;
							$input['img'] = $this->ciupload->file_dst_name;
							$old_img = $this->jp_bakery_model->get_img($input);
							$result = $this->jp_bakery_model->update_img($input);
							if($result==0){
								@unlink(APPPATH.'../assets/image/japanese-bakery/'.$id.'/'.$this->ciupload->file_dst_name);
							}else{
								if($old_img->jpb_img!='' AND file_exists(APPPATH.'../assets/image/japanese-bakery/'.$id.'/'.$old_img->jpb_img)){
									@unlink(APPPATH.'../assets/image/japanese-bakery/'.$id.'/'.$old_img->jpb_img);
								}
							}
						}
						$this->ciupload->clean();
					}
					redirect(base_url()."admin/japanese-bakery");
				}else{
					redirect(base_url()."admin/japanese-bakery");
				}
			}else{
				redirect(base_url()."admin/japanese-bakery");
			}
		}else{
			
			redirect(base_url()."admin/japanese-bakery/add");
		}
	}
	
	public function del($id)
	{
		if($this->session->userdata('admin_logged_in') == FALSE){
			redirect(base_url()."admin/login");
			exit();
		}
		
		if(($id+0)>0){
			$this->load->model('admin/jp_bakery_model','jp_bakery_model');
			if($this->jp_bakery_model->delete($id+0)){
				include(APPPATH.'../assets/function/function.php');
				remove_dir(APPPATH.'../assets/image/japanese-bakery/'.$id);
			}
			redirect(base_url()."admin/japanese-bakery");
		}else{
			redirect(base_url()."admin/japanese-bakery");
		}
	}
}

/* End of file japanese_bakery.php */
/* Location: ./application/controllers/admin/japanese_bakery.php */